package com.cskaoyan.service;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.AdminCreateBO;
import com.cskaoyan.bean.bo.AdminUpdateBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.FileParam;
import com.cskaoyan.bean.po.AdminLogPO;
import com.cskaoyan.bean.po.AdminPO;
import com.cskaoyan.bean.po.MarketLogPO;
import com.cskaoyan.bean.po.RolePO;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.util.EncryptUtils;
import com.cskaoyan.util.StringToArrayUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMapper roleMapper;

    /**
     * lsj
     * 根据 baseParam 和 username查询所有符合条件的管理员
     *
     * @param baseParam page limit sort order
     * @param username  用户名
     * @return 所有符合条件的管理员
     */
    @Override
    public AdminListVO queryAdmin(BaseParam baseParam, String username) {
        AdminListVO adminListVO = new AdminListVO();

        adminListVO.setLimit(baseParam.getLimit());
        adminListVO.setPage(baseParam.getPage());
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<AdminPO> admins = adminMapper.selectAdminByName(baseParam.getSort(), baseParam.getOrder(), username);
        List<AdminVO> adminVos = new ArrayList<>();
        for (AdminPO admin : admins) {
            AdminVO adminVO = new AdminVO();
            adminVO.setAvatar(admin.getAvatar());
            adminVO.setId(admin.getId());
            adminVO.setUsername(admin.getUsername());
            String role_idsStr = admin.getRoleIds();
            List<Integer> integers = StringToArrayUtils.toArray(role_idsStr);
            adminVO.setRoleIds(integers);
            adminVos.add(adminVO);
        }
        PageInfo pageInfo = new PageInfo(admins);
        long total = pageInfo.getTotal();
        adminListVO.setTotal((int) total);
        adminListVO.setList(adminVos);
        return adminListVO;
    }

    /**
     * lsj
     * 根据查询所有符合条件的role
     *
     * @return TVO<RoleVO>
     */
    @Override
    public TVO<RoleVO> queryAllRole() {

        List<RolePO> rolePOS = adminMapper.selectAllRoles();
        PageInfo pageInfo = new PageInfo(rolePOS);

        List<RoleVO> roleVOS = new ArrayList<>();
        for (RolePO rolePO : rolePOS) {
            RoleVO roleVO = new RoleVO();
            roleVO.setLabel(rolePO.getName());
            roleVO.setValue(rolePO.getId());
            roleVOS.add(roleVO);
        }

        TVO<RoleVO> roles = new TVO<>();

        roles.setList(roleVOS);
        roles.setLimit((int) pageInfo.getTotal());
        roles.setPages(1);
        roles.setPage(1);
        roles.setTotal((int) pageInfo.getTotal());
        return roles;
    }

    /**
     * lsj
     *
     * @param adminCreateBO 接收过来的请求参数
     * @return 包含AdminPO的vo对象
     */
    @Override
    public BaseRespVo create(AdminCreateBO adminCreateBO) {

        String newPassword = EncryptUtils.encrypt(adminCreateBO.getPassword());
        AdminPO adminPO = new AdminPO();
        adminPO.setUsername(adminCreateBO.getUsername());
        adminPO.setAvatar(adminCreateBO.getAvatar());
        adminPO.setPassword(newPassword);
        adminPO.setAddTime(new Date());
        adminPO.setUpdateTime(adminPO.getAddTime());
        adminPO.setDeleted(false);
        adminPO.setRoleIds(adminCreateBO.getRoleIds().toString());
        int affectRows = adminMapper.insertAdmin(adminPO);
        AdminCreateVO adminCreateVO = new AdminCreateVO().getAdminCreateVO(adminPO);
        return BaseRespVo.ok(adminCreateVO);

    }

    /**
     * lsj
     *
     * @param adminUpdateBO 接受过来的修改信息
     * @return 返回一个AdminPO对象
     */
    @Override
    public BaseRespVo update(AdminUpdateBO adminUpdateBO) {
        AdminPO adminPO = new AdminPO();
        adminPO.setUsername(adminUpdateBO.getUsername());
        adminPO.setId(adminUpdateBO.getId());
        adminPO.setAvatar(adminUpdateBO.getAvatar());
        adminPO.setRoleIds(adminUpdateBO.getRoleIds().toString());
        String newPassword = EncryptUtils.encrypt(adminUpdateBO.getPassword());
        adminPO.setPassword(newPassword);
        adminPO.setUpdateTime(new Date());
        //adminPO.setAvatar(adminPO.getUsername().replace(FileParam.URL_PRE,""));
        int affectRows = adminMapper.updateAdminById(adminPO);
        //adminPO.setAvatar(FileParam.URL_PRE + adminPO.getUsername());
        AdminCreateVO adminCreateVO = new AdminCreateVO().getAdminCreateVO(adminPO);
        adminCreateVO.setPassword(null);
        return BaseRespVo.ok(adminCreateVO);
    }

    /**
     * lsj
     *
     * @param adminUpdateBO 请求参数
     * @return 逻辑删除
     */
    @Override
    public BaseRespVo delete(AdminUpdateBO adminUpdateBO) {
        int affectRows = adminMapper.logicalDeleteAdmin(adminUpdateBO.getId());
        return BaseRespVo.ok(null);
    }

    /**
     * 李少杰 查看日志文件
     *
     * @return 日志数据
     */
    @Override
    public BaseRespVo queryAllLog(BaseParam param, String name) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<MarketLogPO> logs = adminMapper.selectAllLog(param.getSort(), param.getOrder(), name);
        PageInfo pageInfo = new PageInfo(logs);
        TVO<MarketLogPO> marketLogPOTVO = new TVO<>();
        long total = pageInfo.getTotal();
        marketLogPOTVO.setTotal((int) total);
        marketLogPOTVO.setList(logs);
        marketLogPOTVO.setPage(param.getPage());
        marketLogPOTVO.setLimit(param.getLimit());
        marketLogPOTVO.setPages(pageInfo.getPages());
        return BaseRespVo.ok(marketLogPOTVO);
    }

    @Override
    public List<AdminPO> selectAdminByAdminName(String username) {
        return adminMapper.selectAdminByAdminName(username);
    }

    @Override
    public AdminInfoVO queryInfo(AdminPO adminPO) {
        AdminInfoVO adminInfoVO = new AdminInfoVO();
        adminInfoVO.setName(adminPO.getUsername());
        adminInfoVO.setAvatar(adminPO.getAvatar());
        List<Integer> roleIds = StringToArrayUtils.toArray(adminPO.getRoleIds());
        List<String> perms = new ArrayList<>();
        List<String> roles = new ArrayList<>();
        if (roleIds.contains(1)) {
            perms.add("*");
            adminInfoVO.setPerms(perms);
        } else {
            for (Integer roleId : roleIds) {

                List<String> strings = roleMapper.selectPerIdByRoleId(roleId);
                for (String string : strings) {
                    if (!perms.contains(string)) {
                        perms.add(string);
                    }
                }
                String name = roleMapper.selectRolesNameById(roleId);
                roles.add(name);
            }
            adminInfoVO.setRoles(roles);

            if (perms.contains("*")) {
                ArrayList<String> strings = new ArrayList<>();
                strings.add("*");
                adminInfoVO.setRoles(strings);
                System.out.println(perms);
            } else {
                adminInfoVO.setPerms(perms);
            }
        }
        return adminInfoVO;
    }

    @Override
    public void insertAdminLog(AdminLogPO adminLogPO) {
        adminMapper.insertAdminLog(adminLogPO);
    }

}
